#pragma once
#include <string>
#include <jsoncpp/json/json.h>
#include "Log.hpp"

namespace ResponseModule
{
    using namespace LogModule;

    class Response
    {
    public:
        Response() :_result(0),_code(0){}
        Response(int result, int code) : _result(result), _code(code)
        {
        }
        // 序列化
        bool Serialize(std::string &out_string)
        {
            Json::Value root;
            root["result"] = _result;
            root["code"] = _code;
            out_string = root.toStyledString();
            return true;
        }
        // 反序列化
        bool Deserialize(std::string &in_string)
        {
            Json::Value root;
            Json::Reader reader;
            bool parsingSuccessful = reader.parse(in_string, root);
            if (!parsingSuccessful)
            {
                LOG(LogLevel::ERROR) << "Failed to parse JSON: " << reader.getFormattedErrorMessages();
                return false;
            }
            _result = root["result"].asInt();
            _code = root["code"].asInt();
            return true;
        }
        int Result() { return _result; }
        int Code() { return _code; }
        void SetResult(int result){_result=result;}
        void SetCode(int code){_code=code;}
        ~Response() {}
    
    private:
        int _result; // 结果
        int _code;   // 错误码
    };
}